package optiondialoges;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;
import javax.swing.JDialog;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JTextField;

import panel.GlyphPanel;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.Serializable;

public class UserDefinedOptionDialog extends JDialog implements ActionListener, Serializable {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel buttonPanel = null;
	private JButton okButton = null;
	private JButton cancelButton = null;
	private JPanel jPanel = null;
	private JTextField inputField = null;
	private JButton browseButton = null;
	private GlyphPanel panel;

	/**
	 * @param owner
	 */
	public UserDefinedOptionDialog(Frame owner, GlyphPanel panel) {
		super(owner);
		this.panel = panel;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(276, 134);
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getButtonPanel(), BorderLayout.SOUTH);
			jContentPane.add(getJPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes buttonPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getButtonPanel() {
		if (buttonPanel == null) {
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.insets = new Insets(5, 5, 5, 5);
			buttonPanel = new JPanel();
			buttonPanel.setLayout(new GridBagLayout());
			buttonPanel.add(getOkButton(), gridBagConstraints4);
			buttonPanel.add(getCancelButton(), gridBagConstraints4);
		}
		return buttonPanel;
	}

	/**
	 * This method initializes okButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setText("Ok");
			okButton.addActionListener(this);
		}
		return okButton;
	}

	/**
	 * This method initializes cancelButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setText("Cancel");
			cancelButton.addActionListener(this);			
		}
		return cancelButton;
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.BOTH;
			gridBagConstraints1.insets = new Insets(0, 0, 0, 10);
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints.insets = new Insets(0, 10, 0, 0);
			gridBagConstraints.weightx = 1.0;
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.add(getInputField(), gridBagConstraints);
			jPanel.add(getBrowseButton(), gridBagConstraints1);
		}
		return jPanel;
	}

	/**
	 * This method initializes inputField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getInputField() {
		if (inputField == null) {
			inputField = new JTextField();
			inputField.setPreferredSize(new Dimension(150, 24));
		}
		return inputField;
	}

	/**
	 * This method initializes browseButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBrowseButton() {
		if (browseButton == null) {
			browseButton = new JButton();
			browseButton.setText("Browse");
			browseButton.setPreferredSize(new Dimension(78, 24));
			browseButton.addActionListener(this);
		}
		return browseButton;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JButton button = (JButton) e.getSource();
		if (button.getText().contentEquals("Ok")) {
			if (inputField.getText().contentEquals("")) {
				JOptionPane.showMessageDialog(panel, "Open");
			} else {
				String path="";
				for(int i=0;i<inputField.getText().length();i++)
				{
					char s= inputField.getText().charAt(i);
					if(s=='\\')path+="\\\\";
					else path+=s;
				}
				panel.setDialog(this);
				panel.setCommand(String.format("open(\"%s\");", path));
				panel.setOptionsSet(true);
				
			}
			this.setVisible(false);
//			owner.setEnabled(true);
		} else if (button.getText().contentEquals("Cancel")) {
			this.setVisible(false);
//			owner.setEnabled(true);
		} else if (button.getText().contentEquals("Browse")) {
			String wd = System.getProperty("user.dir");
			JFileChooser fc = new JFileChooser(wd);
			int rc = fc.showDialog(null, "Select Image File");
			if (rc == JFileChooser.APPROVE_OPTION) {
				File file = fc.getSelectedFile();
				inputField.setText(file.getAbsolutePath());
			}
		}

	}

} // @jve:decl-index=0:visual-constraint="10,10"
